<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="选择医院" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wareName">
              <a-select
                v-model="model.warehourse"
                style="width: 100%"
                placeholder="请选择"
                :options="hospitalList"
              ></a-select>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="医院科室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chambeName">
              <a-input v-model="model.chambeName" placeholder="请输入医院科室"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <!--部门分配-->
            <a-form-model-item label="部门分配" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-model="model.departId" :multi="true" @back="backDepartInfo"></j-select-depart>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-radio-group v-model="model.status" name="radioGroup" :default-value="1">
                <a-radio :value="1"> 是 </a-radio>
                <a-radio :value="0"> 否 </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { queryHospitalListApi } from '@/api/container.js'
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
export default {
  name: 'BmsWarehouseForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        code: [{ required: true, message: '请输入仓库编号!' }],
      },
      hospitalList: [], // 医院列表
      tenantsOptions: [],
      url: {
        add: '/bms/bmsHospitalChamber/add',
        edit: '/bms/bmsHospitalChamber/edit',
        queryById: '/bms/bmsHospitalChamber/queryById',
        userWithDepart: '/sys/user/userDepartList',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getHospitalList()
  },
  methods: {
    handleFocusHospital() {
      this.getHospitalList()
    },
    handleFocusDepartment() {
      this.getDepartmentList()
    },
    async getHospitalList() {
      try {
        const r = await queryHospitalListApi({ pageNo: this.pageNo, pageSize: 10000 })
        this.hospitalList = r.result.records.map((e) => {
          return { ...e, label: e.name, value: e.id }
        })
      } catch (err) {
        console.log(err)
        this.$message.error(err)
      }
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    //初始化租户字典
    initTenantList() {
      getAction(this.url.queryTenantList).then((res) => {
        if (res.success) {
          this.tenantsOptions = res.result.map((item, index, arr) => {
            let c = { label: item.name, value: item.id + '' }
            return c
          })
          // console.log('this.tenantsOptions: ', this.tenantsOptions)
        }
      })
    },
    backDepartInfo(info) {
      this.model.departIds = this.model.selecteddeparts
      this.nextDepartOptions = info.map((item, index, arr) => {
        let c = { label: item.text, value: item.value + '' }
        return c
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      console.log(record);
      this.model = Object.assign({}, record)
      console.log(this.model)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
